package com.bright.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.bright.pojo.Blog;
import com.bright.pojo.Type;
import com.bright.service.BlogService;
import com.bright.service.TagService;
import com.bright.service.TypeService;
import com.bright.service.UserService;
import com.bright.utils.RedisUtils;
import com.bright.vo.BlogVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private UserService userService;
    @Autowired
    private TagService tagService;


    @GetMapping("/blogs")
    public Page<Blog> getblogs(@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable){
        return blogService.initBlogs(pageable);
    }

    @GetMapping("/searchblog")
    public Page<Blog> searchblogs(@PageableDefault(size = 8,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable,
                             @RequestParam(required = false) String blog){

//        System.out.println(blog);
        Map map = JSONObject.parseObject(blog);

        String title = (String)map.get("title");
        boolean isrecommand = (boolean)map.get("isrecommend");
        long typeid = Integer.parseInt(map.get("typeid").toString());
        BlogVo blogVo = new BlogVo(title,typeid,isrecommand);

        System.out.println(blogVo.getId()+"---"+blogVo.getTitle()+"+++"+blogVo.isIsrecommand());

        return blogService.getBlogs(pageable,blogVo);
    }


    @PostMapping("/blogs/input")
    public Blog blogsinput(@RequestBody JSONObject getblog){

        String tagids = getblog.getString("tagids");
        Long typeid = getblog.getLong("typeid");
        Long userid = getblog.getLong("userid");

        Blog blog = JSONObject.toJavaObject(getblog,Blog.class);


        blog.setUser(userService.getUserById(blog.getUserid()));
        blog.setTags(tagService.listTag(blog.getTagids()));
        blog.setType(typeService.getType(blog.getTypeid()));

        return blogService.saveBlog(blog);
    }

    @PostMapping("/blogs/update")
    public Blog updateblog(@RequestBody JSONObject getblog){

        String tagids = getblog.getString("tagids");
        Long typeid = getblog.getLong("typeid");

        Blog blog = JSONObject.toJavaObject(getblog,Blog.class);

        blog.setTags(tagService.listTag(tagids));
        blog.setType(typeService.getType(typeid));
        return blogService.updateBlog(blog.getId(),blog);
    }

    @GetMapping("/blogs/delete")
    public String deleteblog(Long id){
        blogService.deleteBlog(id);
        return "success";
    }
}
